//
//  ShareCell.swift
//  Share
//
//  Created by MBA on 2020/10/4.
//  Copyright © 2020 MBA. All rights reserved.
//

import SwiftUI

struct ShareCell: View {
    @ObservedObject var shareStore:ShareDetailStore
    
    func updateSharePrice() {
        shareStore.updatePrice()
    }

    var body: some View {
        
        VStack(alignment: .leading) {
            
       
             Text(shareStore.share.name)
                    .font(.system(size: 20, weight: .bold))
                    .foregroundColor(Color("lable1"))
            
            HStack {
                Text(shareStore.share.code)
                    .foregroundColor(Color("lable2"))
                    
                Spacer()
                Button(action: {updateSharePrice()}, label: {
                    HStack {
                        Text(shareStore.share.price)
                            .font(.system(size: 12))
                        Image(systemName: "goforward")
                            .font(.system(size: 12))
                            .frame(width: 16, height: 16, alignment: .center)
                    }
                    .foregroundColor(Color("danger"))
                })
            }
            .padding(.top)

            Text("\(shareStore.share.getAllNum())")
                .font(.system(size: 20, weight: .bold))
                .foregroundColor(Color("lable1"))
                .padding(.top)
            
            HStack {
              
                
                VStack(alignment: .leading){
                    HStack (alignment:.top ){
                        Text("买")
                            .padding(.all, 4.0)
                            .foregroundColor(Color.white)
                            .background(Color("danger"))
                            .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                        let price:Float = Float(shareStore.share.price) ?? 0
                        if(price>0){
                            let present = shareStore.share.getInPrice()/price
                            Progress( percent: present*100)
                                .frame(width: 22, height: 22, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                .foregroundColor(.grey_2)
                        }
                    }


                    Text("\(shareStore.share.getInPrice(), specifier: "%0.3f")")
                        .foregroundColor(shareStore.needIn ? Color("danger") : Color("lable2"))


                }
                Spacer()
                VStack(alignment: .leading){
                    HStack(alignment:.top ) {
                        Text("卖")
                            .padding(.all, 4.0)

                            .foregroundColor(Color.white)
                            .background(Color("primary"))
                            .cornerRadius(/*@START_MENU_TOKEN@*/3.0/*@END_MENU_TOKEN@*/)
                            .animation(.spring(response: 0.3, dampingFraction: 0.6, blendDuration: 0))
                        
                        
                        if(shareStore.share.getInPrice()>0){
                            let price:Float = Float(shareStore.share.price) ?? 0
                            let present = price/(shareStore.share.getOutPrice())
                            Progress( percent: present*100)
                                .frame(width: 22, height: 22, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
                                .foregroundColor(.grey_2)
                        }
                      
                    }


                    Text("\(shareStore.share.getOutPrice(), specifier: "%0.3f")")
                        .foregroundColor(shareStore.needOut ? Color("danger") : Color("lable2"))

                }
            }
           
            .foregroundColor(Color("background3"))
            .font(.system(size: 14))
        }
        .padding(.all)
        .font(.system(size: 14))
        .background(Color("background3"))
        .cornerRadius(8)
        .shadow(color: Color("shadow"), radius: 4, x: -1, y: 4)
        .onAppear(){
            shareStore.updatePrice()
        }
        
        .alert(item: $shareStore.tostError){tostError in
            Alert(
                title: Text(verbatim: tostError.title),
                message: Text(tostError.msg)
            )
        }
    }
}


struct ShareCell_Previews: PreviewProvider {
    static var previews: some View {
        ShareCell(shareStore: ShareDetailStore(id: 1))
            .previewLayout(.sizeThatFits)
            .preferredColorScheme(.dark)
            .frame(width: 160)
    }
}
